package com.example.lenovo.demo.presenter;

import android.content.Context;
import android.widget.ListView;
import android.widget.Toast;

import com.example.lenovo.demo.R;
import com.example.lenovo.demo.model.Model;
import com.example.lenovo.demo.model.databean.TravelsBean;
import com.example.lenovo.demo.model.idatabean.IModel;
import com.example.lenovo.demo.model.idatabean.OnTravelsSuccess;
import com.example.lenovo.demo.presenter.adapter.Travles_LvAdapter;
import com.example.lenovo.demo.view.ifgview.ITravelsFragment;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/9.
 */

public class TravelsFgPresenter {
    private ITravelsFragment iTravelsFragment;
    private IModel iModel;
    private Context context;
    private Travles_LvAdapter travles_lvAdapter;
    private List<TravelsBean.DataBean> list;
    private PullToRefreshListView listView;

    public TravelsFgPresenter(ITravelsFragment iTravelsFragment) {
        this.iTravelsFragment = iTravelsFragment;
        this.iModel = new Model();
        this.context = iTravelsFragment.getTravelsContext();
        this.listView = iTravelsFragment.getLV();
        this.list = new ArrayList<>();
        this.travles_lvAdapter = new Travles_LvAdapter(list,context);
    }
    public void initData(){
        listView.setAdapter(travles_lvAdapter);
        iModel.sendTravelsBean(context, new OnTravelsSuccess() {
            @Override
            public void TravelsSuccess(List<TravelsBean.DataBean> data) {
                list.addAll(data);
                travles_lvAdapter.notifyDataSetChanged();
            }

            @Override
            public void TravelsFailed() {

            }
        });
        listView.setMode(PullToRefreshBase.Mode.BOTH);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                Toast.makeText(context,"下拉加载",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                Toast.makeText(context,"上拉刷新",Toast.LENGTH_LONG).show();
            }
        });
        //获取下拉刷新的提示内容
        ILoadingLayout ilDown=listView.getLoadingLayoutProxy(true,false);
        ilDown.setPullLabel("快拉");//设置下拉的内容
        ilDown.setRefreshingLabel("亲，正在刷新，请稍等");//设置加载过程中的文本
        ilDown.setReleaseLabel("加载");//设置开始刷新之前的文本
        //设置图标
        ilDown.setLoadingDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher));
        //获取下拉刷新的提示内容
        ILoadingLayout ilup=listView.getLoadingLayoutProxy(false,true);
        ilup.setPullLabel("快拉");//设置下拉的内容
        ilup.setRefreshingLabel("亲，正在刷新，请稍等");//设置加载过程中的文本
        ilup.setReleaseLabel("加载");//设置开始刷新之前的文本
        //设置图标
        ilup.setLoadingDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher));
    }
}
